Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./year2.RDS")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.25, n = 554)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 11.38985 11.44488 11.49897 11.55214 11.60437 11.65567 11.70603 11.75546
##   [9] 11.80395 11.85150 11.89811 11.94378 11.98850 12.03228 12.07511 12.11699
##  [17] 12.15792 12.19790 12.23693 12.27501 12.31211 12.34823 12.38337 12.41753
##  [25] 12.45073 12.48297 12.51426 12.54460 12.57399 12.60245 12.62998 12.65658
##  [33] 12.68227 12.70704 12.73091 12.75387 12.77586 12.79680 12.81672 12.83561
##  [41] 12.85350 12.87040 12.88633 12.90129 12.91531 12.92839 12.94056 12.95183
##  [49] 12.96220 12.97170 12.98034 12.98813 12.99509 13.00123 13.00657 13.01058
##  [57] 13.01284 13.01346 13.01259 13.01035 13.00689 13.00234 12.99682 12.99048
##  [65] 12.98344 12.97584 12.96782 12.95951 12.95103 12.94253 12.93186 12.91715
##  [73] 12.89897 12.87789 12.85446 12.82925 12.80283 12.77575 12.74859 12.72189
##  [81] 12.69624 12.67218 12.65029 12.63113 12.61525 12.59954 12.58084 12.55968
##  [89] 12.53657 12.51206 12.48666 12.46090 12.43530 12.41041 12.38673 12.36480
##  [97] 12.34514 12.32829 12.31476 12.30325 12.29211 12.28134 12.27093 12.26089
## [105] 12.25122 12.24192 12.23299 12.22443 12.21624 12.20842 12.20097 12.19389
## [113] 12.18719 12.18085 12.17489 12.16930 12.16408 12.15923 12.15494 12.15133
## [121] 12.14838 12.14605 12.14429 12.14308 12.14236 12.14210 12.14226 12.14281
## [129] 12.14370 12.14489 12.14635 12.14803 12.15019 12.15307 12.15664 12.16086
## [137] 12.16571 12.17115 12.17714 12.18366 12.19067 12.19814 12.20603 12.21431
## [145] 12.22296 12.23192 12.24118 12.25070 12.26045 12.27039 12.28049 12.29071
## [153] 12.30103 12.31306 12.32820 12.34610 12.36642 12.38882 12.41295 12.43848
## [161] 12.46505 12.49232 12.51995 12.54760 12.57491 12.60156 12.62718 12.65145
## [169] 12.67401 12.69452 12.71265 12.72803 12.74034 12.74922 12.75689 12.76568
## [177] 12.77541 12.78592 12.79703 12.80856 12.82036 12.83224 12.84404 12.85557
## [185] 12.86668 12.87718 12.88691 12.89569 12.90335 12.90973 12.91464 12.91792
## [193] 12.91939 12.91888 12.91623 12.91125 12.90378 12.89300 12.87854 12.86088
## [201] 12.84051 12.81791 12.79356 12.76795 12.74155 12.71486 12.68835 12.66252
## [209] 12.63783 12.61478 12.59385 12.57097 12.54218 12.50816 12.46958 12.42710
## [217] 12.38139 12.33313 12.28299 12.23162 12.17971 12.12792 12.07692 12.02739
## [225] 11.97998 11.93537 11.89423 11.85724 11.82505 11.79833 11.77387 11.74822
## [233] 11.72173 11.69475 11.66762 11.64070 11.61434 11.58887 11.56466 11.54205
## [241] 11.52138 11.50301 11.48729 11.47456 11.46464 11.45698 11.45138 11.44768
## [249] 11.44566 11.44515 11.44596 11.44790 11.45077 11.45439 11.45858 11.46314
## [257] 11.46788 11.47261 11.47715 11.48131 11.48714 11.49649 11.50888 11.52380
## [265] 11.54077 11.55930 11.57888 11.59904 11.61927 11.63908 11.65798 11.67548
## [273] 11.69108 11.70430 11.71712 11.73174 11.74799 11.76565 11.78456 11.80452
## [281] 11.82535 11.84685 11.86883 11.89111 11.91350 11.93581 11.95786 11.97945
## [289] 12.00039 12.02050 12.03960 12.05748 12.07396 12.08970 12.10544 12.12119
## [297] 12.13690 12.15258 12.16820 12.18375 12.19920 12.21454 12.22975 12.24482
## [305] 12.25973 12.27445 12.28898 12.30330 12.31738 12.33121 12.34478 12.35806
## [313] 12.37104 12.38370 12.39624 12.40883 12.42145 12.43406 12.44665 12.45918
## [321] 12.47163 12.48396 12.49614 12.50815 12.51996 12.53154 12.54285 12.55388
## [329] 12.56459 12.57496 12.58563 12.59717 12.60940 12.62214 12.63522 12.64847
## [337] 12.66170 12.67474 12.68742 12.69956 12.71099 12.72152 12.73100 12.73923
## [345] 12.74642 12.75294 12.75882 12.76411 12.76883 12.77303 12.77676 12.78005
## [353] 12.78294 12.78547 12.78769 12.78963 12.79133 12.79283 12.79418 12.79542
## [361] 12.79657 12.79770 12.79882 12.79897 12.79732 12.79417 12.78981 12.78453
## [369] 12.77862 12.77237 12.76608 12.76003 12.75452 12.74985 12.74629 12.74415
## [377] 12.74371 12.74402 12.74396 12.74361 12.74301 12.74224 12.74138 12.74047
## [385] 12.73958 12.73880 12.73816 12.73776 12.73764 12.73787 12.73853 12.73967
## [393] 12.74137 12.74418 12.74847 12.75396 12.76039 12.76750 12.77502 12.78268
## [401] 12.79022 12.79737 12.80386 12.80944 12.81384 12.81679 12.81802 12.81836
## [409] 12.81880 12.81932 12.81989 12.82047 12.82105 12.82159 12.82207 12.82247
## [417] 12.82275 12.82289 12.82286 12.82264 12.82219 12.82150 12.82053 12.81926
## [425] 12.81766 12.81571 12.81325 12.81022 12.80666 12.80263 12.79817 12.79335
## [433] 12.78822 12.78283 12.77723 12.77148 12.76564 12.75975 12.75387 12.74806
## [441] 12.74236 12.73684 12.73155 12.72653 12.72185 12.71755 12.71370 12.70905
## [449] 12.70252 12.69439 12.68494 12.67444 12.66316 12.65140 12.63941 12.62749
## [457] 12.61590 12.60492 12.59484 12.58593 12.57846 12.57271 12.56897 12.56639
## [465] 12.56400 12.56181 12.55983 12.55809 12.55661 12.55541 12.55450 12.55391
## [473] 12.55366 12.55376 12.55423 12.55510 12.55638 12.55839 12.56138 12.56525
## [481] 12.56991 12.57526 12.58122 12.58769 12.59458 12.60179 12.60923 12.61681
## [489] 12.62444 12.63202 12.63946 12.64667 12.65355 12.66002 12.66598 12.67133
## [497] 12.67646 12.68182 12.68739 12.69315 12.69908 12.70519 12.71144 12.71782
## [505] 12.72432 12.73093 12.73762 12.74439 12.75121 12.75808 12.76505 12.77219
## [513] 12.77948 12.78692 12.79452 12.80227 12.81016 12.81820 12.82636 12.83467
## [521] 12.84310 12.85166 12.86034 12.86913 12.87805 12.88707 12.89620 12.90544
## [529] 12.91477 12.92420 12.93372 12.94333 12.95303 12.96281 12.97269 12.98266
## [537] 12.99273 13.00289 13.01315 13.02352 13.03399 13.04457 13.05525 13.06605
## [545] 13.07696 13.08799 13.09914 13.11040 13.12179 13.13330 13.14494 13.15671
## [553] 13.16861 13.18065
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.25, n = 554)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 10.95387 11.02289 11.09083 11.15768 11.22343 11.28807 11.35157 11.41394
##   [9] 11.47515 11.53520 11.59407 11.65174 11.70821 11.76347 11.81749 11.87027
##  [17] 11.92179 11.97204 12.02101 12.06869 12.11509 12.16027 12.20424 12.24701
##  [25] 12.28860 12.32903 12.36832 12.40647 12.44350 12.47944 12.51429 12.54807
##  [33] 12.58080 12.61249 12.64316 12.67283 12.70123 12.72816 12.75365 12.77775
##  [41] 12.80052 12.82199 12.84223 12.86127 12.87917 12.89597 12.91173 12.92649
##  [49] 12.94030 12.95321 12.96527 12.97652 12.98702 12.99682 13.00595 13.01340
##  [57] 13.01829 13.02085 13.02134 13.02002 13.01713 13.01293 13.00766 13.00159
##  [65] 12.99495 12.98801 12.98100 12.97420 12.96783 12.96217 12.95618 12.94875
##  [73] 12.94001 12.93009 12.91911 12.90721 12.89450 12.88112 12.86719 12.85284
##  [81] 12.83820 12.82339 12.80853 12.79377 12.77922 12.76273 12.74246 12.71903
##  [89] 12.69305 12.66513 12.63589 12.60593 12.57588 12.54634 12.51792 12.49124
##  [97] 12.46691 12.44555 12.42776 12.41077 12.39162 12.37062 12.34807 12.32430
## [105] 12.29960 12.27430 12.24870 12.22311 12.19785 12.17323 12.14955 12.12713
## [113] 12.10629 12.08732 12.07055 12.05629 12.04484 12.03651 12.02941 12.02157
## [121] 12.01325 12.00469 11.99612 11.98781 11.97998 11.97289 11.96678 11.96190
## [129] 11.95848 11.95678 11.95704 11.95950 11.96453 11.97221 11.98231 11.99462
## [137] 12.00894 12.02504 12.04271 12.06174 12.08193 12.10304 12.12487 12.14721
## [145] 12.16985 12.19256 12.21514 12.23738 12.25905 12.27996 12.29987 12.31859
## [153] 12.33589 12.35463 12.37750 12.40405 12.43383 12.46638 12.50126 12.53802
## [161] 12.57620 12.61535 12.65502 12.69476 12.73412 12.77265 12.80989 12.84540
## [169] 12.87872 12.90940 12.93700 12.96105 12.98112 12.99674 13.01019 13.02398
## [177] 13.03799 13.05210 13.06617 13.08011 13.09378 13.10707 13.11985 13.13202
## [185] 13.14344 13.15399 13.16357 13.17204 13.17929 13.18520 13.18965 13.19251
## [193] 13.19368 13.19302 13.19043 13.18577 13.17894 13.16941 13.15699 13.14200
## [201] 13.12480 13.10571 13.08509 13.06326 13.04057 13.01736 12.99397 12.97073
## [209] 12.94799 12.92608 12.90535 12.88267 12.85504 12.82300 12.78708 12.74781
## [217] 12.70573 12.66137 12.61526 12.56793 12.51992 12.47176 12.42398 12.37711
## [225] 12.33170 12.28827 12.24735 12.20947 12.17518 12.14500 12.11506 12.08159
## [233] 12.04530 12.00687 11.96700 11.92640 11.88575 11.84575 11.80710 11.77049
## [241] 11.73663 11.70620 11.67991 11.65844 11.63930 11.61966 11.59973 11.57972
## [249] 11.55983 11.54029 11.52130 11.50306 11.48580 11.46972 11.45503 11.44195
## [257] 11.43067 11.42142 11.41440 11.40982 11.40837 11.41030 11.41522 11.42271
## [265] 11.43238 11.44380 11.45658 11.47030 11.48457 11.49897 11.51310 11.52654
## [273] 11.53891 11.54977 11.56128 11.57567 11.59264 11.61191 11.63321 11.65624
## [281] 11.68073 11.70639 11.73294 11.76010 11.78758 11.81511 11.84239 11.86915
## [289] 11.89510 11.91996 11.94344 11.96527 11.98516 12.00478 12.02587 12.04827
## [297] 12.07181 12.09633 12.12166 12.14765 12.17412 12.20092 12.22788 12.25483
## [305] 12.28162 12.30807 12.33403 12.35933 12.38380 12.40729 12.42963 12.45065
## [313] 12.47019 12.48808 12.50477 12.52083 12.53631 12.55126 12.56573 12.57977
## [321] 12.59344 12.60678 12.61984 12.63268 12.64534 12.65789 12.67035 12.68280
## [329] 12.69528 12.70783 12.72021 12.73216 12.74371 12.75490 12.76575 12.77631
## [337] 12.78662 12.79670 12.80659 12.81632 12.82594 12.83547 12.84496 12.85443
## [345] 12.86357 12.87207 12.87997 12.88734 12.89423 12.90067 12.90674 12.91247
## [353] 12.91792 12.92314 12.92819 12.93311 12.93796 12.94278 12.94763 12.95257
## [361] 12.95763 12.96288 12.96837 12.97385 12.97909 12.98410 12.98892 12.99359
## [369] 12.99813 13.00258 13.00696 13.01130 13.01565 13.02003 13.02446 13.02899
## [377] 13.03365 13.03859 13.04394 13.04960 13.05550 13.06157 13.06772 13.07389
## [385] 13.08000 13.08596 13.09170 13.09715 13.10222 13.10685 13.11095 13.11445
## [393] 13.11728 13.12021 13.12399 13.12841 13.13327 13.13839 13.14357 13.14861
## [401] 13.15332 13.15750 13.16096 13.16351 13.16495 13.16508 13.16372 13.16104
## [409] 13.15740 13.15290 13.14762 13.14163 13.13502 13.12788 13.12028 13.11231
## [417] 13.10404 13.09557 13.08698 13.07834 13.06975 13.06128 13.05301 13.04503
## [425] 13.03743 13.03027 13.02265 13.01366 13.00346 12.99221 12.98004 12.96710
## [433] 12.95356 12.93955 12.92522 12.91073 12.89622 12.88184 12.86774 12.85408
## [441] 12.84099 12.82862 12.81714 12.80668 12.79739 12.78943 12.78294 12.77688
## [449] 12.77021 12.76304 12.75550 12.74770 12.73977 12.73182 12.72399 12.71639
## [457] 12.70914 12.70237 12.69620 12.69074 12.68612 12.68246 12.67989 12.67841
## [465] 12.67790 12.67825 12.67935 12.68110 12.68338 12.68608 12.68912 12.69236
## [473] 12.69571 12.69906 12.70230 12.70532 12.70802 12.71088 12.71442 12.71857
## [481] 12.72329 12.72853 12.73421 12.74030 12.74673 12.75345 12.76040 12.76752
## [489] 12.77478 12.78209 12.78942 12.79671 12.80389 12.81092 12.81774 12.82429
## [497] 12.83080 12.83752 12.84444 12.85156 12.85886 12.86636 12.87403 12.88188
## [505] 12.88990 12.89808 12.90642 12.91490 12.92354 12.93231 12.94125 12.95037
## [513] 12.95967 12.96915 12.97882 12.98866 12.99868 13.00889 13.01926 13.02982
## [521] 13.04054 13.05144 13.06251 13.07376 13.08517 13.09675 13.10849 13.12041
## [529] 13.13249 13.14473 13.15714 13.16971 13.18246 13.19539 13.20849 13.22177
## [537] 13.23523 13.24886 13.26266 13.27664 13.29080 13.30513 13.31964 13.33432
## [545] 13.34918 13.36421 13.37942 13.39480 13.41035 13.42608 13.44198 13.45806
## [553] 13.47431 13.49073
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.25, n = 554)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 10.90639 10.96224 11.01721 11.07129 11.12447 11.17675 11.22813 11.27859
##   [9] 11.32814 11.37676 11.42445 11.47120 11.51702 11.56188 11.60579 11.64874
##  [17] 11.69072 11.73173 11.77176 11.81081 11.84889 11.88601 11.92220 11.95745
##  [25] 11.99176 12.02514 12.05761 12.08916 12.11979 12.14952 12.17835 12.20628
##  [33] 12.23333 12.25949 12.28477 12.30917 12.33253 12.35471 12.37572 12.39562
##  [41] 12.41443 12.43220 12.44896 12.46475 12.47961 12.49356 12.50666 12.51893
##  [49] 12.53041 12.54114 12.55115 12.56049 12.56918 12.57727 12.58479 12.59118
##  [57] 12.59595 12.59922 12.60113 12.60182 12.60141 12.60004 12.59784 12.59495
##  [65] 12.59149 12.58761 12.58343 12.57908 12.57471 12.57044 12.56471 12.55613
##  [73] 12.54509 12.53196 12.51713 12.50098 12.48389 12.46625 12.44843 12.43082
##  [81] 12.41381 12.39776 12.38307 12.37012 12.35928 12.34872 12.33650 12.32288
##  [89] 12.30813 12.29251 12.27629 12.25973 12.24309 12.22664 12.21064 12.19536
##  [97] 12.18105 12.16799 12.15643 12.14549 12.13413 12.12240 12.11038 12.09810
## [105] 12.08564 12.07306 12.06040 12.04773 12.03510 12.02259 12.01023 11.99810
## [113] 11.98624 11.97472 11.96360 11.95293 11.94278 11.93320 11.92316 11.91181
## [121] 11.89943 11.88631 11.87274 11.85901 11.84541 11.83223 11.81975 11.80827
## [129] 11.79808 11.78946 11.78271 11.77810 11.77452 11.77069 11.76670 11.76265
## [137] 11.75862 11.75471 11.75101 11.74761 11.74460 11.74207 11.74011 11.73882
## [145] 11.73829 11.73861 11.73986 11.74215 11.74556 11.75018 11.75611 11.76343
## [153] 11.77224 11.78361 11.79832 11.81604 11.83646 11.85923 11.88403 11.91054
## [161] 11.93841 11.96733 11.99697 12.02699 12.05708 12.08689 12.11610 12.14439
## [169] 12.17141 12.19686 12.22039 12.24168 12.26040 12.27622 12.29208 12.31094
## [177] 12.33246 12.35628 12.38209 12.40954 12.43828 12.46800 12.49833 12.52896
## [185] 12.55953 12.58972 12.61919 12.64758 12.67458 12.69984 12.72302 12.74379
## [193] 12.76180 12.77672 12.78822 12.79594 12.79957 12.79887 12.79423 12.78613
## [201] 12.77511 12.76167 12.74633 12.72960 12.71199 12.69401 12.67618 12.65902
## [209] 12.64303 12.62873 12.61663 12.60311 12.58456 12.56152 12.53452 12.50412
## [217] 12.47084 12.43522 12.39782 12.35916 12.31978 12.28023 12.24104 12.20275
## [225] 12.16590 12.13104 12.09870 12.06942 12.04373 12.02219 12.00190 11.97986
## [233] 11.95644 11.93201 11.90694 11.88158 11.85632 11.83151 11.80753 11.78475
## [241] 11.76352 11.74422 11.72721 11.71287 11.70011 11.68763 11.67551 11.66381
## [249] 11.65260 11.64195 11.63191 11.62257 11.61398 11.60621 11.59933 11.59341
## [257] 11.58851 11.58469 11.58203 11.58059 11.58147 11.58544 11.59210 11.60100
## [265] 11.61173 11.62386 11.63698 11.65065 11.66445 11.67797 11.69077 11.70244
## [273] 11.71254 11.72066 11.72819 11.73673 11.74617 11.75643 11.76742 11.77902
## [281] 11.79116 11.80373 11.81665 11.82980 11.84310 11.85645 11.86976 11.88293
## [289] 11.89586 11.90847 11.92064 11.93230 11.94334 11.95469 11.96729 11.98098
## [297] 11.99565 12.01115 12.02736 12.04414 12.06136 12.07890 12.09660 12.11436
## [305] 12.13202 12.14946 12.16656 12.18316 12.19915 12.21439 12.22875 12.24209
## [313] 12.25429 12.26521 12.27555 12.28606 12.29667 12.30732 12.31793 12.32844
## [321] 12.33878 12.34889 12.35870 12.36814 12.37714 12.38564 12.39357 12.40086
## [329] 12.40745 12.41327 12.41788 12.42102 12.42290 12.42373 12.42371 12.42305
## [337] 12.42195 12.42062 12.41927 12.41809 12.41729 12.41709 12.41768 12.41928
## [345] 12.42109 12.42223 12.42278 12.42282 12.42240 12.42161 12.42052 12.41919
## [353] 12.41771 12.41614 12.41455 12.41301 12.41161 12.41040 12.40947 12.40887
## [361] 12.40870 12.40901 12.40988 12.41049 12.41010 12.40888 12.40703 12.40471
## [369] 12.40211 12.39942 12.39681 12.39446 12.39256 12.39128 12.39081 12.39133
## [377] 12.39301 12.39596 12.40004 12.40510 12.41099 12.41756 12.42466 12.43213
## [385] 12.43982 12.44759 12.45527 12.46272 12.46979 12.47633 12.48218 12.48718
## [393] 12.49120 12.49546 12.50110 12.50788 12.51553 12.52379 12.53241 12.54112
## [401] 12.54967 12.55780 12.56524 12.57175 12.57705 12.58090 12.58303 12.58400
## [409] 12.58458 12.58477 12.58461 12.58412 12.58331 12.58222 12.58087 12.57928
## [417] 12.57747 12.57546 12.57329 12.57096 12.56851 12.56596 12.56333 12.56064
## [425] 12.55792 12.55519 12.55189 12.54750 12.54213 12.53591 12.52893 12.52132
## [433] 12.51318 12.50462 12.49577 12.48671 12.47758 12.46848 12.45952 12.45081
## [441] 12.44247 12.43460 12.42732 12.42074 12.41497 12.41012 12.40631 12.40279
## [449] 12.39880 12.39442 12.38973 12.38482 12.37977 12.37465 12.36956 12.36456
## [457] 12.35975 12.35520 12.35100 12.34723 12.34396 12.34129 12.33929 12.33802
## [465] 12.33744 12.33747 12.33801 12.33898 12.34029 12.34185 12.34358 12.34539
## [473] 12.34720 12.34891 12.35044 12.35171 12.35262 12.35340 12.35432 12.35539
## [481] 12.35659 12.35793 12.35941 12.36103 12.36278 12.36466 12.36667 12.36880
## [489] 12.37107 12.37346 12.37597 12.37861 12.38136 12.38423 12.38722 12.39032
## [497] 12.39346 12.39657 12.39968 12.40283 12.40604 12.40935 12.41279 12.41639
## [505] 12.42019 12.42420 12.42848 12.43303 12.43791 12.44314 12.44862 12.45426
## [513] 12.46005 12.46599 12.47210 12.47836 12.48479 12.49138 12.49815 12.50508
## [521] 12.51219 12.51947 12.52694 12.53458 12.54241 12.55043 12.55863 12.56703
## [529] 12.57563 12.58442 12.59341 12.60262 12.61206 12.62174 12.63164 12.64177
## [537] 12.65212 12.66268 12.67346 12.68445 12.69565 12.70705 12.71865 12.73044
## [545] 12.74243 12.75460 12.76696 12.77951 12.79223 12.80512 12.81819 12.83142
## [553] 12.84482 12.85837
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")

keeping in case

#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")